<?php
//   +----------------------------------------------------------------------
//   | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//   | Licensed ( http://www.hdphp.cn/licenses/ )
//   | Author: Jack <sophia2152@qq.com>
//   | 官方网址: http://www.hdphp.cn
//   | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//   | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//   +----------------------------------------------------------------------
namespace app\controller\admin;

use app\common\services\system\admin\SystemAdminServices;
use app\Request;
use hdphp\services\CacheService;
use think\facade\Cache;

/**
 * Class：系统后台登陆
 * 创建人：Jack
 * 创建时间：2024/1/12 14:04
 * @package app\controller\admin
 */
class Login
{
    /**
     * 构造函数
     * @param SystemAdminServices $services
     */
    public function __construct(SystemAdminServices $services)
    {
        $this->services = $services;
    }

    /**
     * 注释：登录系统
     * 创建人：Jack
     * 创建时间：2024/1/12 14:07
     * @param Request $request
     * @return mixed
     */
    public function login(Request $request)
    {
        [$username, $password, $captchaType, $captchaVerification] = $request->postMore([
            'username',
            'password',
            ['captchaType', ''],
            ['captchaVerification', ''],
        ], true);

        $key = 'login_identifying_code_' . $username;

//        if (Cache::has($key) && Cache::get($key) > 2) {
//            if (!$captchaType || !$captchaVerification) {
//                return app('json')->fail('请拖动滑块验证');
//            }
//            // 二次验证
//            try {
//                aj_captcha_check_two($captchaType, $captchaVerification);
//            } catch (\Throwable $e) {
//                return app('json')->fail($e->getError());
//            }
//        }
        validate(\app\validate\admin\setting\SystemAdminValidate::class)->scene('get')->check(['username' => $username, 'password' => $password]);
        $res = $this->services->login($username, $password, 'admin');
        if ($res) {
            Cache::delete($key);
        }
        return app('json')->success($res);
    }

    /**
     * 注释：短信验证码登录
     * 创建人：Jack
     * 创建时间：2024/1/12 15:26
     * @param Request $request
     * @return mixed
     */
    public function mobileLogin(Request $request)
    {
        [$phone, $code] = $request->postMore([
            'phone', 'code',
        ], true);
        if (!$code) {
            return app('json')->fail('请输入验证码');
        }

        // 验证验证码
        $verifyCode = CacheService::get('code_' . $phone);
        if (!$verifyCode) return app('json')->fail('请先获取验证码');
        $verifyCode = substr($verifyCode, 0, 6);
        if ($verifyCode != $code) {
            CacheService::delete('code_' . $phone);
            return app('json')->fail('验证码错误');
        }

        return app('json')->success($this->services->login($phone, $code, 'admin', true));
    }

    /**
     * 注释：短信重置密码
     * 创建人：Jack
     * 创建时间：2024/1/12 15:27
     * @param Request $request
     * @return mixed
     */
    public function resetPwd(Request $request)
    {
        [$phone, $code, $newPassword] = $request->postMore([
            'phone', 'code', 'new_password'
        ], true);
        if (!$phone) {
            return app('json')->fail('请输入手机号');
        }
        if (!check_phone($phone)) {
            return app('json')->fail('请输入正确的手机号!');
        }
        if (!$code) {
            return app('json')->fail('请输入验证码');
        }
        if (!$newPassword) {
            return app('json')->fail('请输入新密码');
        }
        // 验证验证码
        $verifyCode = CacheService::get('code_' . $phone);
        if (!$verifyCode) return app('json')->fail('请先获取验证码');
        $verifyCode = substr($verifyCode, 0, 6);
        if ($verifyCode != $code) {
            CacheService::delete('code_' . $phone);
            return app('json')->fail('验证码错误');
        }
        $this->services->resetPassword($phone, $newPassword);
        return app('json')->success('重置成功，请重新登录!');
    }


}